#pragma once

#include <string>

namespace ns_util
{
    class EnDecodeUtil
    {
    private:
        static int hex2Dec(char hex)
        {
            if (hex >= '0' && hex <= '9')
                return hex - '0';
            if ((hex >= 'a' && hex <= 'f'))
                return hex - 'a' + 10;
            if ((hex >= 'A' && hex <= 'F'))
                return hex - 'A' + 10;
            return 0;
        }

    public:
        static std::string Decode(const std::string &src)
        {
            std::string dst;
            // Linux%E5%E3
            for (int i = 0; i < src.size(); ++i)
            {
                // LOG(DEBUG) << i << ": " << src[i] << std::endl;
                //  %后面两个字符 16进制数 isxdigit判断字符是否为16进制
                if (src[i] == '%' && isxdigit(src[i + 1]) && isxdigit(src[i + 2]))
                {
                    // 16进制数转10进制
                    dst += hex2Dec(src[i + 1]) * 16 + hex2Dec(src[i + 2]);
                    i += 2;
                }
                else
                {
                    dst += src[i];
                }
                // LOG(DEBUG) << i << ": " << dst.back() << "\n\n";
            }

            return std::move(dst);
        }
    };
    class StringUtil
    {
    public:
        static bool CutString(const std::string &s, std::string &sub1_out, std::string &sub2_out, std::string sep)
        {
            size_t pos = s.find(sep);
            if (pos != std::string::npos)
            {
                sub1_out = s.substr(0, pos);
                sub2_out = s.substr(pos + sep.size());
                return true;
            }

            return false;
        }
    };
}